<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class FastCommand extends Command
{
    protected static $defaultName = 'fast';

    protected function configure()
    {
        $this
            ->setDescription('Add a short description for your command')
            ->addArgument('entityName', InputArgument::OPTIONAL, 'Argument entityName')
            ->addArgument('actionNum', InputArgument::OPTIONAL, 'Argument actionNum')
            ->addArgument('all', InputArgument::OPTIONAL, 'Argument all')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $entityName = $input->getArgument('entityName');
        $io->note(sprintf('您输入的实体名: %s', $entityName));
        $actionNum = (int)$input->getArgument('actionNum');
        $io->note(sprintf('您输入的步骤: %s', $actionNum));

        $all = $input->getArgument('all');
        $all = $all?true:false;
        $io->note(sprintf('是否全部生成: %s', $all?"true":"false"));

        switch ($actionNum) {
            case 3:
                $command = $this->getApplication()->find('lazy:mo:create');

                if ($all) {
                    $root = str_replace("src\Command","",__DIR__);//E:\www\symfony4\src\Command
                    $dirEntity = $root."src\Entity";is_dir($dirEntity) OR mkdir($dirEntity, 0777, true);
                    //查找Entity
                    $entityList = scandir($dirEntity);
                    foreach ($entityList as $item) {
                        if (substr($item,-3) !== 'php') {
                            continue;
                        }
                        $arguments = array(
                            'command'       => 'lazy:mo:create',
                            'en'      => substr($item,0,-4),
                        );
                        $greetInput = new ArrayInput($arguments);
                        $command->run($greetInput, $output);
                    }
                }else{
                    $arguments = array(
                        'command'       => 'lazy:mo:create',
                        'en'      => $entityName,
                    );
                    $greetInput = new ArrayInput($arguments);
                    $command->run($greetInput, $output);
                }
                break;
            case 4:
                $command = $this->getApplication()->find('lazy:mo:move');
                if ($all) {
                    dd(__DIR__);
                    $root = str_replace("src\Command","",__DIR__);//E:\www\symfony4\src\Command
                    $dirEntity = $root."src\Entity";is_dir($dirEntity) OR mkdir($dirEntity, 0777, true);
                    //查找Entity
                    $entityList = scandir($dirEntity);
                    foreach ($entityList as $item) {
                        if (substr($item,-3) !== 'php') {
                            continue;
                        }
                        $arguments = array(
                            'command'       => 'lazy:mo:move',
                            'en'      => substr($item,0,-4),
                        );
                        $greetInput = new ArrayInput($arguments);
                        $command->run($greetInput, $output);
                    }
                }else{
                    $arguments = array(
                        'command'       => 'lazy:mo:move',
                        'en'      => $entityName,
                    );
                    $greetInput = new ArrayInput($arguments);
                    $command->run($greetInput, $output);
                }
                break;
        }


        $io->success('Success '.$entityName);

        return 0;
    }
}
